<?php

/**
 * ECSHOP 商品分类页
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: testyang $
 * $Id: category.php 15013 2008-10-23 09:31:42Z testyang $
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');

	$get_sort  = !empty($_REQUEST['sort']) ? $_GET['sort'] : 'goods_id';
	$get_order  = !empty($_REQUEST['order']) ? $_GET['order'] : 'DESC';
	$uu = array();
	
//	if ($_SESSION['user_id'] > 0)
//	{
//		$smarty->assign('user_name', $_SESSION['user_name']);
//	
//	}
	$brand_s=array();
	foreach(range('A','Z') as $letter){ 
   		 array_push($brand_s,$letter);
	}
 	array_push($brand_s,"0-9");
		
    $sql = "SELECT b.brand_id, b.brand_name,b.brand_other_name,b.brand_short,b.brand_logo,COUNT(*) AS goods_num ".
            "FROM " . $GLOBALS['ecs']->table('brand') . "AS b, ".
                $GLOBALS['ecs']->table('goods') . " AS g LEFT JOIN ". $GLOBALS['ecs']->table('goods_cat') . " AS gc ON g.goods_id = gc.goods_id " .
            "WHERE g.brand_id = b.brand_id AND b.is_show = 1 " .
            " AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ".
            "GROUP BY b.brand_id HAVING goods_num > 0 ORDER BY b.brand_short, b.sort_order";

    $brands = $db->GetAll($sql);
	
	$p ="";
	$brand_list = array();	
	$key_list = array();		
	foreach ($brands as $key => $brand)
	{
		$brand['brand_logo'] = OSS_BAND.get_image_path($brand['brand_id'],$brand['brand_logo'],false,'brand');		
//		$brand['brand_logo'] = OSS_HOST.get_image_path($brand['brand_id'],"");				

		$brand['brand_group'] = "";
		if ($brand['brand_short'] != $p){
			$brand['brand_group'] = $brand_s[$brand['brand_short']];
			array_push($key_list,array("key"=>$brand_s[$brand['brand_short']]));			
		}
		$p = $brand['brand_short'];
		
		unset($brand['goods_num']);
				
		$brand_list[$key] = $brand;		
	}
	//var_dump($uu);
	$result['dataSet'] = $brand_list;
	$result['key_brands'] = $key_list;	
	
	//热门品牌
    $sql = "SELECT b.brand_id, b.brand_name,b.brand_other_name,b.brand_logo ".
            "FROM " . $GLOBALS['ecs']->table('brand') . "AS b ".
            "WHERE b.is_hot > 0 ORDER BY b.brand_short, b.sort_order";

    $hot_brands = $db->GetAll($sql);
	
	foreach ($hot_brands as $key => $brand)
	{
		$brand['brand_logo'] = OSS_BAND.get_image_path($brand['brand_id'],$brand['brand_logo'],false,'brand');		
		if ($brand['brand_other_name'])
		{
			$brand['brand_name'] = $brand['brand_other_name'];					
		}
		unset($brand['brand_other_name']);
//		$brand['brand_logo'] = OSS_HOST.get_image_path($brand['brand_id'],"");

		$hot_brands[$key] = $brand;		
	}
	$result['hot_brands'] = $hot_brands;	
	//获得默认品牌
//	$first_brand = 0;
//	if (count($brand_list)>0){
//		$first_brand = $brand_list[0]['brand_id'];		
//	}	
//	if (count($hot_brands)>0){
//		$first_brand = $hot_brands[0]['brand_id'];		
//	}
//	//对应商品列表
//  $order_rule = 'ORDER BY g.goods_id desc, g.sort_order';
//
//	$cat_goods = get_brand_goods($first_brand, 0, 0, $order_rule);
//	foreach ($cat_goods as $key => $goods_data)
//	{
//		$goods_data['goods_img'] = OSS_HOST.get_image_path($goods_data['goods_id'],$goods_data['goods_img'],false,'goods');
//		$goods_data['goods_img'] = OSS_HOST.get_image_path($goods_data['goods_id'],"");
//		$cat_goods[$key] = $goods_data;
//	}
//	$result["goods"] = $cat_goods;
	echo json_encode($result);

?>